//https://leetcode.cn/problems/validate-ip-address/solutions/947700/mian-shi-guan-xiang-kan-dao-shi-yao-qie-d23jw/
#include <iostream>
#include <vector>
#include <list>
#include <sstream>

using namespace std;

class Solution {
public:
    string validIPAddress(string IP) {
        if(is4(IP))return "IPv4";
        else if(is6(IP))return "IPv6";
        return "Neither";
    }
    bool is4(string IP){
        vector<string>ip;
        split(IP,ip,'.');
        if(ip.size()!=4)return false;
        for(string s:ip){
            if(s.size()==0||(s.size()>1&&s[0]=='0')||s.size()>3)return false;
            for(char c:s){
                if(!isdigit(c))return false;
            }
            int digit=stoi(s);
            if(digit<0||digit>255)return false;
        }
        return true;
    }

    bool is6(string IP){
        vector<string>ip;
        split(IP,ip,':');
        if(ip.size()!=8)return false;
        for(string s:ip){
            if(s.size()==0||s.size()>4)return false;
            for(char c:s){
                if(c<'0'||c>'9'&&c<'A'||c>'F'&&c<'a'||c>'f')return false;
            }
        }
        return true;
    }
    // void split(string s,vector<string>&ip,char c){
    //     stringstream ss(s);
    //     string tmp;
    //     while(getline(ss,tmp,c))ip.push_back(tmp);
    //     if(s.size()>0&&s.back()==c)ip.push_back({});
    // }
    void split(string s,vector<string>&ip,char c){
      size_t pos=0;
      while(pos=s.find(c)!=std::string::npos){
        std::string tmp_str=s.substr(0,pos);
        s.erase(0,pos+1); //这个地方容易出错的
        ip.emplace_back(tmp_str);
      }
      ip.emplace_back(s);
    }
};
